package cn.jiahua.hospital.utils;

import cn.jiahua.hospital.config.SmsConfig;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName SMSUtil
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2021/4/1 12:31
 * @Version 1.0
 **/
@Component
public class SMSUtil {

    @Autowired
    private SmsConfig smsConfig;


    /**
     * LTAIh3VhtOA0l9v5
     * Z9zV2b2we2x5pBRRoVdCry5TnJPSGZ
     * 使用AK&SK初始化账号Client
     *
     * @return Client
     * @throws Exception
     */
    public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(smsConfig.getAccessKeyId())
                // 您的AccessKey Secret
                .setAccessKeySecret(smsConfig.getAccessKeySecret());
        // 访问的域名
        config.endpoint = smsConfig.getDomain();
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


    public SendSmsResponse sendSms(String phone, String templateParams) throws Exception {

        com.aliyun.dysmsapi20170525.Client client = createClient();
        //封装请求对象
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(smsConfig.getSignName())
                .setTemplateCode(smsConfig.getTemplateCode())
                .setTemplateParam(templateParams);

        // 复制代码运行请自行打印 API 的返回值
        return client.sendSms(sendSmsRequest);
    }


}
